package com.gg.zjj.service.impl;

import com.gg.common.mapper.DFileMapper;
import com.gg.common.mapper.MApplyMapper;
import com.gg.common.pojo.DFile;
import com.gg.common.pojo.MApply;
import com.gg.zjj.service.ProductionPlanRegistrationService;
import com.gg.zjj.view.MapplyView;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author koco
 * 2022/10/11
 * 14:35
 */
@Service
public class ProductionPlanRegistrationServiceImpl implements ProductionPlanRegistrationService {
    @Resource
    private DFileMapper dFileMapper;

    @Resource
    private MApplyMapper applyMapper;

    @Override
    public List<DFile> InquireAllProductsService() {
        return dFileMapper.InquireAllProducts();
    }

    @Override
    public void addProductionPlanService(String jsonArray) {
        JSONArray jsonArray1 = JSONArray.fromObject(jsonArray);
        MapplyView view = new MapplyView((List<MApply>) JSONArray.toCollection(jsonArray1, MApply.class));
        String id = applyMapper.queryTheLastProductionPlanNumber();
        if (null == id) {
            for (MApply mApply : view.getMApplyList()) {
                mApply.setApplyId(productionPlanNumber()+"0001");
            }
        }else{
            String serialNumber=id.substring(id.length()-4);
            serialNumber= String.valueOf(Integer.parseInt(serialNumber)+1);
            while (serialNumber.length()<4){
                serialNumber="0"+serialNumber;
            }
            for (MApply mApply : view.getMApplyList()) {
                mApply.setApplyId(productionPlanNumber()+serialNumber);
            }
        }
        applyMapper.addProductionPlanMapper(view);
    }


    private String productionPlanNumber() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(date);
        format1 = "300" + format1.replace("-", "");
        return format1;
    }
}
